package com.yizhuoyan.his.serviceimpl.department;

import com.yizhuoyan.his.algorithm.SnowFlake64Id;
import com.yizhuoyan.his.ao.DepartmentCategoryAo;
import com.yizhuoyan.his.dao.HosDepartmentCategoryDao;
import com.yizhuoyan.his.entity.HosDepartmentCategoryEntity;
import com.yizhuoyan.his.exception.HISIllegalParameterException;
import com.yizhuoyan.his.service.department.DepartmentCategoryManageService;
import com.yizhuoyan.his.servicesupport.DatabaseIdGenerateSupportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.validation.constraints.NotNull;

import java.util.HashMap;
import java.util.Map;
@Service
public class DepartmentCategoryManageServiceImpl implements DepartmentCategoryManageService {


    @Autowired
    DatabaseIdGenerateSupportService databaseIdGenerateSupportService;

    @Autowired
    HosDepartmentCategoryDao dao;

    @Override
    public void addDepartmentCategory(DepartmentCategoryAo ao) throws Exception {
        //参数验证
        //执行业务逻辑
        HosDepartmentCategoryEntity entity=new HosDepartmentCategoryEntity();
        entity.setShowOrder(ao.getShowOrder());
        entity.setName(ao.getName());
        entity.setCode(ao.getCode());
        entity.setId(databaseIdGenerateSupportService.nextId());
        dao.insert(entity);
        //返回结果

    }

    @Override
    public void modifyDepartmentCategory(@NotNull Long id,DepartmentCategoryAo ao) throws Exception {

        //参数验证
        if(ao.getCode().trim().length()==0 && ao.getName().trim()==null && ao.getShowOrder().trim()==null ){
            throw new HISIllegalParameterException("请输入要修改的内容");
        }
        //执行业务逻辑
        Map<String,Object> map=new HashMap<>();
        map.put("name",ao.getName());
        map.put("code",ao.getCode());
        map.put("showOrder",ao.getShowOrder());
        dao.update(id,map);

    }

    @Override
    public HosDepartmentCategoryEntity queryDepartmentCategoryById(@NotNull Long id) throws Exception {


        HosDepartmentCategoryEntity entity=dao.selectById(id);
        return entity;
    }

    @Override
    public void removeDepartmentCategoryById(@NotNull Long id) throws Exception {
        if (id==0){
            throw  new HISIllegalParameterException("id不能为空");
        }
        dao.delete(id);
    }
}
